package com.giantest.ink;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

/**
 * 128M Ink 点除rule
 * 读取txt文件
 */
public class InkTxt {
    //要读取处理的文件统一放在D:\ink目录下
    public static String dir = "D:\\ink";
    //处理后的新文件
    public static String newDir = "D:\\new_ink";

    public static void main(String[] args) {
        File inkDir = new File(dir);
        String[] arr = inkDir.list();
        for (int i = 0; i < arr.length; i++) {
            String fileName = arr[i];
            //遍历目录下的每一个txt文件进行处理
            if (fileName.endsWith("txt") || fileName.endsWith("TXT")) {
                String content = getTxt(fileName);
//                System.out.println(content);
                writeToNewTxt(content, fileName);
            }
        }
    }

    /**
     * 读取每个txt的内容，截出要处理的map部分，处理后返回新的完整内容
     * @param fileName
     */
    public static String getTxt(String fileName) {
        FileReader fileReader = new FileReader(dir+"\\"+fileName);
        String result = fileReader.readString();
        String head = result.substring(0, result.indexOf("]")+3);
        String mapTxt = result.substring(result.indexOf("]")+3);
        String foot = mapTxt.substring(mapTxt.indexOf("[")-2);
        mapTxt = mapTxt.substring(0, mapTxt.indexOf("[")-2);
//        System.out.println(mapTxt);
        //处理map
        String newMap = handle(mapTxt);
//        System.out.println(newMap);
        String content = head + newMap + foot;
        return content;
    }

    /**
     * 根据点除规则进行处理，返回处理后的文本内容
     * @param map
     * @return
     */
    public static String handle(String map) {
        //将要处理的map转换成一个二维数组
        String[] sArr = map.split("\r\n");
        int maxLength = 0;
        for (int i = 0; i < sArr.length; i++) {
//            System.out.println("第"+i+"行长度:"+sArr[i].length());
            if (sArr[i].length() > maxLength) {
                maxLength = sArr[i].length();
            }
        }
//        System.out.println("maxlength:"+maxLength);
        char[][] chars = new char[sArr.length][maxLength];
        for (int i = 0; i < sArr.length; i++) {
            for (int j = 0; j < sArr[i].length(); j++) {
                chars[i][j] = sArr[i].charAt(j);
            }
        }
        //用来保存需要点除的坐标
        Set<String> coordinate = new HashSet<String>();
        InkRule.rule1(chars, coordinate);
        InkRule.rule2(chars, coordinate);
//        System.out.println(Arrays.deepToString(chars));
//        System.out.println(coordinate);
        String newMap = ink(coordinate, chars);
        return newMap;
    }

    /**
     * 根据坐标进行点除，将坐标周围8个点中为1的全部设为W
     * @param coordinate
     * @param arr
     * @return
     */
    public static String ink(Set<String> coordinate, char[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (coordinate.contains(i+"_"+j)) {
                    for (int k = i-1; k <= i+1; k++) {
                        for (int l = j-1; l <= j+1; l++) {
                            if (k >= 0 && k < arr.length && l >=0 && l < arr[k].length) {
                                if (arr[k][l] == '1') {
                                    arr[k][l] = 'W';
                                }
                            }
                        }
                    }
                }
            }
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                stringBuffer.append(arr[i][j]);
            }
            stringBuffer.append("\r\n");
        }
        return stringBuffer.toString();
    }

    /**
     * 将处理后的内容写入到新文本
     * @param content
     */
    public static void writeToNewTxt(String content, String fileName) {
        FileWriter writer = new FileWriter(newDir+"\\"+"NEW_"+fileName);
        writer.write(content);
    }
}
